import { defineStore } from 'pinia'
import { nanoid } from "nanoid";
import axios from "axios";

// export const useLoveTalkStore = defineStore('talk', {
//     actions: {
//         async getAtalk() {
//             let { data } = await axios.get(
//                 "https://api.uomg.com/api/rand.qinghua?format=json"
//             );
//             console.log("result.data.content==", data.content);
//             let obj = { id: nanoid(), title: data.content };
//             console.log("obj===", obj);
//             this.talkList.unshift(obj);
//         }
//     },

//     //真正存储数据的地方
//     state() {
//         return {
//             talkList: JSON.parse(sessionStorage.getItem("talkList") as string) || []
//         }
//     }
// })


//组合式写法
import { reactive } from "vue";
export const useLoveTalkStore = defineStore('talk', () => {
    //talkList相当于state
    const talkList = reactive(JSON.parse(sessionStorage.getItem("talkList") as string) || [])
    //getAtalk相当于action
    async function getAtalk() {
        let { data } = await axios.get(
            "https://api.uomg.com/api/rand.qinghua?format=json"
        );
        console.log("result.data.content==", data.content);
        let obj = { id: nanoid(), title: data.content };
        console.log("obj===", obj);
        talkList.unshift(obj);
    }
    return {talkList,getAtalk}
})